package cn.com.zsw.gblog.client;

import com.alibaba.fastjson.JSON;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author ：shiwangzhou
 * @Date ：Created in 2020-09-23 14:49
 * @Description： 请求拦截器
 */
@Component
@Slf4j
public class ForestInterceptor<T> implements Interceptor<T> {
    @Override
    public void onError(ForestRuntimeException e, ForestRequest forestRequest, ForestResponse forestResponse) {

    }

    @Override
    public void onSuccess(T data, ForestRequest request, ForestResponse response) {
        log.info("forest请求成功返回的结果:" + JSON.toJSONString(data));
        if (data.toString().contains("error")) {
            throw new ForestRuntimeException("forest请求返回报错");
        }
    }
}
